#include <rclcpp/rclcpp.hpp>
#include <geometry_msgs/msg/twist.hpp>
#include <rosbag2_cpp/writer.hpp>
class SimpleBagRecorderNode: public rclcpp::Node
{
private:
    std::shared_ptr<rosbag2_cpp::Writer> writer_;
    rclcpp::Subscription<geometry_msgs::msg::Twist>::SharedPtr sub_;
public:
    SimpleBagRecorderNode():Node("simple_bag_recorder_node")
    {
      RCLCPP_INFO(this->get_logger(),"SimpleBagRecorderNode创建");
      this->writer_ = std::make_unique<rosbag2_cpp::Writer>();
      // 相对工作空间
      this->writer_->open("my_bag");
      
      this->sub_=this->create_subscription<geometry_msgs::msg::Twist>("/turtle1/cmd_vel",10,
      std::bind(&SimpleBagRecorderNode::do_write_msg,this,std::placeholders::_1));
    }
private:
    void do_write_msg(std::shared_ptr<rclcpp::SerializedMessage> msg)
    {
        RCLCPP_INFO(this->get_logger(),"数据写出");
        this->writer_->write(msg,"/turtle1/cmd_vel","geometry_msgs/msg/Twist",this->now());
    }    
};
int main(int argc, char ** argv)
{
    rclcpp::init(argc,argv);
    rclcpp::spin(std::make_shared<SimpleBagRecorderNode>());
    rclcpp::shutdown();
    return 0;
}